package guru.springframework.services;


import guru.springframework.domain.ProductB;
import guru.springframework.mappers.ProductMapper;
import guru.springframework.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    private ProductRepository productRepository;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    public void setProductRepository(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    @Override
    public List<ProductB> listAllProducts() {
        return productMapper.selectAll();
    }

    @Override
    public ProductB getProductById(Integer id) {
        return productMapper.selectProductById(id);
    }

    @Override
    public int deleteProductById(Integer id) {
        return productMapper.deleteProductById(id);
    }

    @Override
    public void saveProduct(ProductB product) {
        if (product.getId() == null) {
            productMapper.insertProduct(product);
        } else {
            productMapper.updateProduct(product);
        }
    }
}
